package com.dev.ac.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dev.ac.entity.AActive;
import com.dev.ac.entity.AAdmin;
import com.dev.ac.entity.AAuthentication;
import com.dev.ac.entity.ARole;
import com.dev.ac.exception.WebException;
import com.dev.ac.service.AActiveService;
import com.dev.ac.service.AAdminService;
import com.dev.ac.service.AAuthenticationService;
import com.dev.ac.mapper.AAuthenticationMapper;
import com.dev.ac.service.ARoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author object
 * @description 针对表【a_authentication】的数据库操作Service实现
 * @createDate 2024-04-14 16:13:24
 */
@Service
public class AAuthenticationServiceImpl extends ServiceImpl<AAuthenticationMapper, AAuthentication>
		implements AAuthenticationService {

	@Autowired
	AAdminService aAdminService;
	@Autowired
	AActiveService aActiveService;
	@Autowired
	ARoleService aRoleService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean authentication(List<Integer> activeId) {
		AAdmin aAdmin = aAdminService.getCurrentUser();
		System.out.println(aAdmin);
		ARole aRole = aRoleService.getById(aAdmin.getRoleId());
		if (aRole.getRoleGrade() == 2) {
			throw new WebException("抱歉，你没有权限进行认证");
		}
		List<AAuthentication> list = new ArrayList<>();
		for (Integer id : activeId) {
			// 修改状态
			AActive active = aActiveService.getById(id);
			AAuthentication authentication = this.getOne(new QueryWrapper<AAuthentication>().eq("active_id", active.getId()));
			Assert.isNull(authentication, "当前活动已认证");
			AAuthentication aAuthentication = new AAuthentication();
			aAuthentication.setActiveId(active.getId());
			aAuthentication.setStatus(0);
			aAuthentication.setCreateTime(LocalDateTime.now());
			aAuthentication.setAdminId(aAdmin.getId());
			list.add(aAuthentication);
		}


		return this.saveBatch(list);
	}

	@Override
	public Object serlectAll() {
		// 已认证的数据
		List<AAuthentication> list = this.list();
		List<Integer> ids = list.stream().map(AAuthentication::getActiveId).collect(Collectors.toList());
		// 查询所有需要认证的数据
		List<AActive> aActives = aActiveService.list(new QueryWrapper<AActive>()
				.eq("is_delete", 0)
				.eq("status", 2)
		);

		// 已认证的活动
		List<AActive> isAuthentic = aActives.stream().filter(item -> ids.indexOf(item.getId()) != -1).collect(Collectors.toList());
		System.out.println(isAuthentic);
		// 未认证的活动
		List<AActive> noAuthentic = aActives.stream().filter(item -> ids.indexOf(item.getId()) != 1).collect(Collectors.toList());
		System.out.println(noAuthentic);
		return MapUtil.builder()
				.put("list", aActives)
				.put("isAuthentic", isAuthentic)
				.put("noAuthentic", noAuthentic)
				.build();
	}
}




